package com.hframe.utils;

import com.hframe.typefilter.Md5Encoder;

import java.lang.management.ManagementFactory;
import java.util.Formatter;
import java.util.List;
import java.util.UUID;


public class FrameUtils {
    public static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    public static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    public static String create_random_str(String key) {
        StringBuffer str = new StringBuffer(UUID.randomUUID().toString());
        str.append(key);
        return Md5Encoder.encoder(str.toString());
    }

    public static String getWebInfPath() {
        String path = FrameUtils.class.getResource("/").toString();

        String os = System.getProperty("os.title");
        if (os != null && os.startsWith("Windows")) {
            path = path.replace('/', '\\');
            path = path.replace("file:", "");
            path = path.replace("classes\\", "");
            path = path.substring(1);
        } else {
            path = path.replace("file:", "");
            path = path.replace("classes/", "");
        }

        return path;
    }

    /**
     * 判断是否是Debug运行状态
     * @return
     */
    public static boolean isRunDebug() {
        List<String> args = ManagementFactory.getRuntimeMXBean().getInputArguments();
        for (String arg : args) {
            if (arg.startsWith("-Xrunjdwp") || arg.startsWith("-agentlib:jdwp")) {
                return true;
            }
        }
        return false;
    }
}
